<template>
  <div>
    <el-card class="box-card">
      <!--查询-->
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="方向名称">
          <el-input v-model="formInline.name" placeholder="方向名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button type="primary" @click="toAdd">新增</el-button>
        </el-form-item>
      </el-form>


      <el-table :data="tabledata" style="width: 100%">
        <el-table-column prop="id" label="编号" width="180"></el-table-column>
        <el-table-column prop="name" label="方向名称" width="180"></el-table-column>
        <el-table-column fixed="right" label="操作">
          <template v-slot:="scope">
            <el-button @click="handleClick(scope.row)" type="text" size="small">编辑</el-button>
            <el-button type="text" @click="deleteCollege(scope.row.id)" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>


      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="formInline.page"
        :page-sizes="[4, 5, 6, 7]"
        :page-size="formInline.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>


      <el-dialog title="修改" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item label="方向名称" :label-width="formLabelWidth">
            <el-input v-model="form.id" autocomplete="off"></el-input>
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="updateCollege">确 定</el-button>
        </div>
      </el-dialog>

    </el-card>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        tabledata: [],
        formInline: {
          name: '',
          page: 1,
          size: 4
        },
        total: 10,
        dialogFormVisible: false,
        formLabelWidth: '120px',
        form: {
          id: 0,
          name: ''
        }
      }
    },
    created() {
      this.showList()
    },
    methods: {
      deleteCollege(id) {
        this.$axios.get('college/delete?id=' + id).then((res) => {
          this.$message({
            message: '恭喜你，删除成功',
            type: 'success'
          });
          this.showList()
        })
      },
      updateCollege() {
        this.$axios.post('college/update', this.form).then((res) => {
          console.log(res);
          this.$message({
            message: '恭喜你，修改成功',
            type: 'success'
          });
          this.dialogFormVisible = false;
          this.showList()
        })
      },

      handleClick(row) {
        this.dialogFormVisible = true;
        this.form.id = row.id;
        this.form.name = row.name
      },

      toAdd() {
        this.$router.push("CollegeAdd")
      },

      showList() {
        this.$axios.post("college/searchPage", this.formInline).then((res) => {
          this.tabledata = res.data.data.records;
          this.total = res.data.data.total
        })
      },

      onSubmit() {
        this.formInline.page = 1;
        this.showList()
      },

      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.formInline.size = val;
        this.showList();
      },

      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.formInline.page = val;
        this.showList();
      }
    }
  }

</script>


<style>


</style>
